﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AccidentCards
{
    public partial class PersonForm : Form
    {
        private long accidentCardId;
        private long personCardId;
        public PersonForm( long _accidentCardId )
        {
            InitializeComponent();
            accidentCardId = _accidentCardId;
            personCardId = -1;
        }

        public PersonForm(long _accidentCardId, long _personCardId)
        {
            InitializeComponent();
            accidentCardId = _accidentCardId;
            personCardId = _personCardId;
        }

        private void LoadComboBoxes()
        {
            Dictionary.LoadComboBoxValues(accidentMemberType, Dictionary.DictCategories.AccidentMemberTypes);
            Dictionary.LoadComboBoxValues(escape, Dictionary.DictCategories.Escape);
            Dictionary.LoadComboBoxValues(gender, Dictionary.DictCategories.Gender);
            Dictionary.LoadComboBoxValues(socialType, Dictionary.DictCategories.SocialType);
            Dictionary.LoadComboBoxValues(consequencesType, Dictionary.DictCategories.ConsequencesType);
            Dictionary.LoadComboBoxValues(nation, Dictionary.DictCategories.Nation);
            Dictionary.LoadComboBoxValues(violationsOfTrafficLaw, Dictionary.DictCategories.ViolationsOfTrafficLaw);
            
        }
        private void PersonForm_Load(object sender, EventArgs e)
        {
            LoadComboBoxes();
            if (personCardId != -1)
            {
                try
                {
                    DataTable dtCard;
                    dtCard = PersonCard.GetInfoById(personCardId);

                    accidentMemberType.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["accidentMemberType"]);
                    vehicleNumber.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["vehicleNumber"]);
                    escape.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["escape"]);
                    fullName.Text = dtCard.Rows[0]["fullName"].ToString();
                    driverLicenceId.Text = dtCard.Rows[0]["driverLicenceId"].ToString();
                    gender.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["gender"]);
                    birthday.Value = Convert.ToDateTime(dtCard.Rows[0]["birthday"]);
                    driverExperience.Text = dtCard.Rows[0]["driverExperience"].ToString();
                    drivingTime.Text = dtCard.Rows[0]["drivingTime"].ToString();
                    socialType.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["socialType"]);
                    consequencesType.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["consequencesType"]);
                    nation.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["nation"]);
                    livingPlace.Text = dtCard.Rows[0]["livingPlace"].ToString();
                    violationsOfTrafficLaw.SelectedValue = Convert.ToInt32(dtCard.Rows[0]["violationsOfTrafficLaw"]);                   

                }
                catch
                {
                    MessageBox.Show("Ошибка в  LoadPersonForm");
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (personCardId == -1)
            {
                PersonCard.CreatePersonCard(ref personCardId, accidentCardId);
            }
            DateTime birthdayDateTime = new DateTime(birthday.Value.Year, birthday.Value.Month, birthday.Value.Day,
                                                  birthday.Value.Hour, birthday.Value.Minute, birthday.Value.Second);

            PersonCard.UpdateCard( personCardId,
                                            Convert.ToInt32(accidentMemberType.SelectedValue),
	                                        Convert.ToInt32(vehicleNumber.SelectedValue),
	                                        Convert.ToInt32(escape.SelectedValue),
	                                        fullName.Text,
	                                        driverLicenceId.Text,
	                                        Convert.ToInt32(gender.SelectedValue),
                                            birthdayDateTime,
	                                        Convert.ToInt32(driverExperience.Text),
	                                        Convert.ToInt32(drivingTime.Text),
	                                        Convert.ToInt32(socialType.SelectedValue),
	                                        Convert.ToInt32(consequencesType.SelectedValue),
	                                        Convert.ToInt32(nation.SelectedValue),
	                                        livingPlace.Text,
	                                        Convert.ToInt32(violationsOfTrafficLaw.SelectedValue) );
            this.Close();
        }

    }
}
